package com.xu.cloud.codegenerator.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.cloud.codegenerator.convert.QueryUserInfoByIdRespConverter;
import com.xu.cloud.codegenerator.pojo.domain.UserInfoDO;
import com.xu.cloud.codegenerator.pojo.http.req.QueryUserInfoByIdReq;
import com.xu.cloud.codegenerator.pojo.http.req.UserInfoPageReq;
import com.xu.cloud.codegenerator.pojo.http.resp.QueryUserInfoByIdResp;
import com.xu.cloud.codegenerator.pojo.http.resp.UserInfoPageResp;
import com.xu.cloud.codegenerator.service.UserInfoService;
import com.xu.cloud.common.pojo.Result;
import com.xu.cloud.common.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xuguan
 * @since 2024/1/26
 */
@RestController
@RequestMapping("/sys/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/list")
    public Result<UserInfoPageResp> list(@ModelAttribute UserInfoPageReq pageReq) {
        final Page<UserInfoDO> queryPage = PageUtil.mybatisPlusPage();
        final IPage<UserInfoDO> page = userInfoService.page(queryPage, null);
        final UserInfoPageResp pageResp = PageUtil.mybatisPlusPageToPageResp(page, UserInfoPageResp.class);
        return Result.ok(pageResp);
    }

    @GetMapping("/info/{id}")
    public Result<QueryUserInfoByIdResp> info(QueryUserInfoByIdReq req) {
        final UserInfoDO userInfoDo = userInfoService.getById(req.getId());
        final QueryUserInfoByIdResp resp = QueryUserInfoByIdRespConverter.convertUserInfoDO(userInfoDo);
        return Result.ok(resp);
    }
}